home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / chrome / content / related_searches.js < prev    next >
Text File  |  2010-01-12  |  21KB  |  510 lines

  1. //Note to editor: NOT executed from XUL
  2. //Copyright 2009 Yongqian Li.
  3. (function()
  4. {
  5.     //var $ = smarterwiki_$;
  6.     var safe_SW_getBoolPref = function(name)
  7.     {
  8.         try
  9.         {
  10.             return SW_getBoolPref(name);
  11.         }
  12.         catch(err)
  13.         {
  14.             return null;
  15.         }
  16.     };
  17.     
  18.     var setBoolPref = function(pref_name, value, callback)
  19.     {
  20.         SW_setBoolPref("extensions.smarterwiki." + pref_name, value);
  21.         if(callback)
  22.         {
  23.             callback(value);
  24.         }
  25.     };
  26.  
  27.     var getBoolPref = function(pref_name, callback)
  28.     {
  29.         if(callback)
  30.         {
  31.             callback(safe_SW_getBoolPref("extensions.smarterwiki." + pref_name));
  32.         }
  33.     };
  34.     if(window.chrome && window.chrome.extension) // detect chrome
  35.     {
  36.         var get_localStorage = function(key, callback) //only used for Chrome
  37.         {    
  38.             chrome.extension.sendRequest({"msg_type": "get_localStorage", "key": key}, function(response)
  39.             {
  40.                 if(callback)
  41.                 {
  42.                     callback(response.value);
  43.                 }
  44.             });
  45.         };
  46.         var set_localStorage = function(key, value, callback) //only used for Chrome
  47.         {    
  48.             chrome.extension.sendRequest({"msg_type": "set_localStorage", "key": key, "value": value}, function(response)
  49.             {
  50.                 if(callback)
  51.                 {
  52.                     callback(response.status);
  53.                 }
  54.             });
  55.         };
  56.  
  57.         setBoolPref = function(pref_name, value, callback)
  58.         {
  59.             set_localStorage("pref." + pref_name, value, callback);
  60.         };
  61.  
  62.         getBoolPref = function(pref_name, callback)
  63.         {
  64.             get_localStorage("pref." + pref_name, function(str)
  65.             {
  66.                 if(callback) {
  67.                     callback(str == "true");
  68.                 }
  69.             });
  70.         };
  71.         
  72.         SW_$ = $;
  73.         SW_LOG = function(){};
  74.         $.get = function(url, data, callback, type)
  75.         {
  76.             chrome.extension.sendRequest({"msg_type": "$.get", "url": url, "data": data, "type": type}, function(response)
  77.             {
  78.                 if(callback)
  79.                 {
  80.                     callback(response.data, response.textStatus);
  81.                 }
  82.             });            
  83.         };
  84.     }
  85.     
  86.     
  87.     var also_search_on = window.also_search_on || "Also search on: ";
  88.     var confirm_disable = window.confirm_disable || "Are you sure you want to disable this?\n\nTo re-enable, go to Tools -> SmarterFox";
  89.     var disable_str = window.disable_label || "disable";
  90.     
  91.  
  92.     
  93.     var get_log_msg_url = function(msg)
  94.     {
  95.         msg["rand"] = parseInt(Math.random() * 1000000000);
  96.         var params = [];
  97.         for(var k in msg)
  98.         {
  99.             params.push(encodeURIComponent(k) + "=" + encodeURIComponent(msg[k]));
  100.         }
  101.         /*
  102.         if("https:" == document.location.protocol)
  103.         {
  104.             return "https://ssl.msgs.smarterfox.com/log_msg?" + params.join("&");
  105.         }
  106.         */
  107.         return "http://msgs.smarterfox.com/log_msg?" + params.join("&");
  108.     };
  109.     
  110.  
  111.     var log_msg_async = function(msg, callback)
  112.     {
  113.         var $ting = $('<img style="display: none;" />');
  114.         if(callback)
  115.         {
  116.             $ting.load(callback);
  117.         }
  118.         $ting.attr("src", get_log_msg_url(msg));
  119.         return $ting;
  120.     };
  121.  
  122.     var track_click = function($a, msg)
  123.     {
  124.         $a.mouseup(function(event)
  125.         {
  126.             var original_href = $a.attr("href");
  127.             msg["redirect_to"] = original_href;
  128.             $a.attr("href", get_log_msg_url(msg));
  129.  
  130.             setTimeout(function()
  131.             {
  132.                 $a.attr("href", original_href);
  133.             }, 10);
  134.         });
  135.     };        
  136.     
  137.     
  138.  
  139.     var add_related_searches = function(doc)
  140.     {
  141.         var getSearchResultsURL = function(url, terms)
  142.         {
  143.             var language = navigator.language ? navigator.language : navigator.userLanguage;
  144.             var url = url.replace(/{searchTerms}/g, encodeURIComponent(terms));
  145.             url = url.replace(/{language}/g, language);
  146.             return url;
  147.         };
  148.         
  149.         var yandex_lang_codes = ['ru', 'rus',
  150.             'uk', 'ukr',
  151.             'be', 'bel',
  152.             'kk', 'kaz',
  153.             'uz', 'uzb',
  154.             'kaa', 'kaa',
  155.             'tk', 'tuk',
  156.             'tut', 'gag',
  157.             'mo', 'mol',
  158.             'ky', 'kir',
  159.             'et', 'est',
  160.             'lt', 'lit',
  161.             'lv', 'lav',
  162.             'hy', 'arm', 'hye', 
  163.             'az', 'aze',
  164.             'ka', 'geo', 'kat',
  165.             'tg', 'tgk'];
  166.         
  167.         var show_yandex = false;
  168.         for(var x = 0; x < yandex_lang_codes.length; x++)
  169.         {
  170.             if(navigator.language == yandex_lang_codes[x] || navigator.language.indexOf(yandex_lang_codes[x] + "-") == 0)
  171.             {
  172.                 show_yandex = true;
  173.             }
  174.         }
  175.         
  176.  
  177.         var searchWikipediaURL = "http://smarterfox.com/wikisearch/search?q={searchTerms}&locale={language}";
  178.         var searchOneRiotURL = "http://www.oneriot.com/search?p=smarterfox&ssrc=also_search_on&spid=8493c8f1-0b5b-4116-99fd-f0bcb0a3b602&q={searchTerms}";
  179.         var searchYandexURL = "http://yandex.ru/yandsearch?text={searchTerms}&clid=127505";
  180.         var searchDeliciousURL = "http://delicious.com/search?p={searchTerms}";
  181.         var searchTwitterURL = "http://search.twitter.com/search?q={searchTerms}";
  182.         var tweetThisURL = "http://twitter.com/home?status={searchTerms}";
  183.         var searchBingURL = "http://www.bing.com/search?q={searchTerms}";
  184.         var searchBaiduURL = "http://www.baidu.com/s?wd={searchTerms}";
  185.         var searchRedditURL = "http://www.reddit.com/search?q={searchTerms}";
  186.         var searchYouTubeURL = "http://www.youtube.com/results?search_query={searchTerms}";
  187.         var searchGoogleURL = "http://www.google.com/search?q={searchTerms}";
  188.         var searchAmazonURL = "http://www.amazon.com/s?ie=UTF8&index=blended&field-keywords={searchTerms}&tag=smtfx1-20";
  189.         var searchYahooURL = "http://search.yahoo.com/search?p={searchTerms}";
  190.         var searchSurfCanyonURL = "http://search.surfcanyon.com/search?f=nrl1&q={searchTerms}&partner=fastestfox";
  191.         
  192.  
  193.         var get_$related_searches_inline = function(queryTerms, addSearchEngines, se_class)
  194.         {
  195.             //log_msg_async({name: "related_searches_inserted", source: "smarterfox", type: "inline"});
  196.  
  197.             var $container = $('<span id="related-searches" class="inline related-searches-' + se_class + '"></span>', doc);
  198.  
  199.             var $delete_button = $('<a href="#" title="' + disable_str + '" id="smarterwiki-disable-button"></a>')
  200.                 .click(function(event)
  201.                 {
  202.                     if(doc.defaultView.confirm(confirm_disable))
  203.                     { 
  204.                         setBoolPref("add_related_searches", false);
  205.                         $container.fadeOut("normal", function()
  206.                             {
  207.                                 $container.remove();
  208.                             });
  209.                     }
  210.                     return false;
  211.                 });
  212.                 
  213.     
  214.             var addSearchEngine = function(favicon, title, searchURL)
  215.             {
  216.                 var $a = $('<a class="search-engine-link" href="' + searchURL + '" title="' + title + '">' + 
  217.                             '<img class="search-engine-favicon" src="' + favicon + '" alt="' + title + '" />' + 
  218.                             '</a>'
  219.                           ).attr("target", "_blank");
  220.                 $a.appendTo($container);
  221.                 track_click($a, {name: "related_searches_searched", "title": title, source: "smarterfox"});
  222.                 return $a;
  223.             }
  224.     
  225.             addSearchEngines(addSearchEngine);
  226.             
  227.             $delete_button.appendTo($container);            
  228.             return $container;
  229.         };
  230.  
  231.         var get_$related_searches = function(queryTerms, addSearchEngines, se_class)
  232.         {
  233.             //log_msg_async({name: "related_searches_inserted", source: "smarterfox"});
  234.  
  235.             var $container = $('<div id="related-searches" class="boxed related-searches-' + se_class + '"></div>', doc);
  236.             
  237.             var $top_container = $('<div class="smarterwiki-clearfix"></div>', doc).appendTo($container);
  238.  
  239.             $('<label id="related-searches-label" title="More searches">' + also_search_on + '</label>').appendTo($top_container);
  240.  
  241.             var $delete_button = $('<a href="#" title="' + disable_str + '" id="smarterwiki-disable-button"></a>')
  242.                 .click(function(event)
  243.                 {
  244.                     if(doc.defaultView.confirm(confirm_disable))
  245.                     { 
  246.                         setBoolPref("add_related_searches", false);
  247.                         $container.slideUp("normal", function()
  248.                             {
  249.                                 $container.remove();
  250.                             });
  251.                     }
  252.                     return false;
  253.                 })
  254.                 .appendTo($top_container);
  255.  
  256.            
  257.             var $ulist = $('<ul class="smarterwiki-clearfix" id="related-searches-list"></ul>').appendTo($container);
  258.             
  259.             var addSearchEngine = function(favicon, title, searchURL)
  260.             {
  261.                 var $a = $('<a class="search-engine-link" href="' + searchURL + '" title="' + title + '">' + 
  262.                             '<img class="search-engine-favicon" src="' + favicon + '" alt="' + title + '" />' + 
  263.                             '<span class="search-engine-label">' + title + '</span>' + 
  264.                             '</a>'
  265.                           ).attr("target", "_blank");
  266.                 $('<li class="related-searches-list-item"></li>').append(
  267.                     $a
  268.                 ).appendTo($ulist);
  269.                 track_click($a, {name: "related_searches_searched", "title": title, source: "smarterfox"});
  270.                 return $a;
  271.             }
  272.             
  273.             addSearchEngines(addSearchEngine);            
  274.             return $container;
  275.         };
  276.         
  277.  
  278.         var googleURLRegExp = new RegExp("^http://www.google.(?:[^/]*)/(?:search\\?|#)(?:.*&)?q=([^&=]*)(.*)$");
  279.         var match = googleURLRegExp.exec(doc.location.href);
  280.         if(match)
  281.         {
  282.             //alert(doc.location.href + " matched");
  283.             var insert_into_google = function(match)
  284.             {
  285.                 var queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  286.                 get_$related_searches(queryTerms, function(addSearchEngine)
  287.                 {
  288.                     var $a = addSearchEngine(
  289.                         'http://www.amazon.com/favicon.ico', 
  290.                         'Amazon', 
  291.                         getSearchResultsURL(searchAmazonURL, queryTerms));
  292.                     $a.click(function()
  293.                     {
  294.                         SW_openNewTab($(this).attr("href"));
  295.                         return false;
  296.                     });
  297.                     addSearchEngine(
  298.                         'http://surfcanyon.com/favicon.ico', 
  299.                         'Surf Canyon', 
  300.                         getSearchResultsURL(searchSurfCanyonURL, queryTerms));
  301.                     addSearchEngine(
  302.                         'http://static.smarterfox.com/media/popup_bubble/oneriot-favicon.ico', 
  303.                         'OneRiot', 
  304.                         getSearchResultsURL(searchOneRiotURL, queryTerms));
  305.                     if(show_yandex)
  306.                     {
  307.                         addSearchEngine(
  308.                             'http://www.yandex.ru/favicon.ico', 
  309.                             '\ufeff\u042f\u043d\u0434\u0435\u043a\u0441', 
  310.                             getSearchResultsURL(searchYandexURL, queryTerms));
  311.                     }
  312.                     else
  313.                     {
  314.                         addSearchEngine(
  315.                             'http://www.yahoo.com/favicon.ico', 
  316.                             'Yahoo', 
  317.                             getSearchResultsURL(searchYahooURL, queryTerms));
  318.                     }
  319.                     addSearchEngine(
  320.                         'http://twitter.com/favicon.ico', 
  321.                         'Twitter', 
  322.                         getSearchResultsURL(searchTwitterURL, queryTerms));
  323.                 }, "google").insertAfter($("#ssb", doc));                
  324.             };
  325.  
  326.             document.addEventListener("DOMNodeInserted", function()
  327.             {
  328.                 if($("#ssb", doc).length > 0 && $("#related-searches").length == 0)
  329.                 {
  330.                     insert_into_google(googleURLRegExp.exec(doc.location.href));
  331.                 }
  332.             }, false);
  333.  
  334.             return;
  335.         }
  336.  
  337.         var yahooURLRegExp = new RegExp("^http://search.yahoo.com/search[^?]*\\?(?:.*&)?p=([^&=]*)(.*)$");
  338.         match = yahooURLRegExp.exec(doc.location.href);
  339.         if(match)
  340.         {
  341.             var queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  342.             get_$related_searches(queryTerms, function(addSearchEngine)
  343.             {
  344.                 var $a = addSearchEngine(
  345.                     'http://www.amazon.com/favicon.ico', 
  346.                     'Amazon', 
  347.                     getSearchResultsURL(searchAmazonURL, queryTerms));
  348.                 $a.click(function()
  349.                 {
  350.                     SW_openNewTab($(this).attr("href"));
  351.                     return false;
  352.                 });
  353.                 addSearchEngine(
  354.                     'http://surfcanyon.com/favicon.ico', 
  355.                     'Surf Canyon', 
  356.                     getSearchResultsURL(searchSurfCanyonURL, queryTerms));
  357.                 addSearchEngine(
  358.                     'http://static.smarterfox.com/media/popup_bubble/oneriot-favicon.ico', 
  359.                     'OneRiot', 
  360.                     getSearchResultsURL(searchOneRiotURL, queryTerms));
  361.                 if(show_yandex)
  362.                 {
  363.                     addSearchEngine(
  364.                         'http://www.yandex.ru/favicon.ico', 
  365.                         '\ufeff\u042f\u043d\u0434\u0435\u043a\u0441', 
  366.                         getSearchResultsURL(searchYandexURL, queryTerms));
  367.                 }
  368.                 else
  369.                 {
  370.                     addSearchEngine(
  371.                         'http://www.google.com/favicon.ico', 
  372.                         'Google', 
  373.                         getSearchResultsURL(searchGoogleURL, queryTerms));
  374.                 }
  375.                 addSearchEngine(
  376.                     'http://twitter.com/favicon.ico', 
  377.                     'Twitter', 
  378.                     getSearchResultsURL(searchTwitterURL, queryTerms));
  379.             }, "yahoo").prependTo($("#main", doc));
  380.         }
  381.  
  382.  
  383.         var bingURLRegExp = new RegExp("^http://www.bing.com/search[^?]*\\?(?:.*&)?q=([^&=]*)(.*)$");
  384.         match = bingURLRegExp.exec(doc.location.href);
  385.         if(match)
  386.         {
  387.             var queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  388.             get_$related_searches(queryTerms, function(addSearchEngine)
  389.             {
  390.                 var $a = addSearchEngine(
  391.                     'http://www.amazon.com/favicon.ico', 
  392.                     'Amazon', 
  393.                     getSearchResultsURL(searchAmazonURL, queryTerms));
  394.                 $a.click(function()
  395.                 {
  396.                     SW_openNewTab($(this).attr("href"));
  397.                     return false;
  398.                 });
  399.                 addSearchEngine(
  400.                     'http://surfcanyon.com/favicon.ico', 
  401.                     'Surf Canyon', 
  402.                     getSearchResultsURL(searchSurfCanyonURL, queryTerms));
  403.                 addSearchEngine(
  404.                     'http://static.smarterfox.com/media/popup_bubble/oneriot-favicon.ico', 
  405.                     'OneRiot', 
  406.                     getSearchResultsURL(searchOneRiotURL, queryTerms));
  407.                 if(show_yandex)
  408.                 {
  409.                     addSearchEngine(
  410.                         'http://www.yandex.ru/favicon.ico', 
  411.                         '\ufeff\u042f\u043d\u0434\u0435\u043a\u0441', 
  412.                         getSearchResultsURL(searchYandexURL, queryTerms));
  413.                 }
  414.                 else
  415.                 {
  416.                     addSearchEngine(
  417.                         'http://www.yahoo.com/favicon.ico',
  418.                         'Yahoo',
  419.                         getSearchResultsURL(searchYahooURL, queryTerms));
  420.                 }
  421.                 addSearchEngine(
  422.                     'http://twitter.com/favicon.ico', 
  423.                     'Twitter', 
  424.                     getSearchResultsURL(searchTwitterURL, queryTerms));
  425.             }, "bing").prependTo($("#results_container", doc));
  426.         }
  427.  
  428.  
  429.         var baiduURLRegExp = new RegExp("^http://www.baidu.com/s[^?]*\\?(?:.*&)?wd=([^&=]*)(.*)$");
  430.         match = baiduURLRegExp.exec(doc.location.href);
  431.         if(match)
  432.         {
  433.             var queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  434.             get_$related_searches(queryTerms, function(addSearchEngine)
  435.             {
  436.                 addSearchEngine(
  437.                     'http://static.smarterfox.com/media/popup_bubble/oneriot-favicon.ico',
  438.                     'OneRiot',
  439.                     getSearchResultsURL(searchOneRiotURL, queryTerms));
  440.                 addSearchEngine(
  441.                     'http://www.yahoo.com/favicon.ico',
  442.                     'Yahoo',
  443.                     getSearchResultsURL(searchYahooURL, queryTerms));
  444.                 var $a = addSearchEngine(
  445.                     'http://www.amazon.com/favicon.ico', 
  446.                     'Amazon', 
  447.                     getSearchResultsURL(searchAmazonURL, queryTerms));
  448.                 $a.click(function()
  449.                 {
  450.                     SW_openNewTab($(this).attr("href"));
  451.                     return false;
  452.                 });
  453.                 addSearchEngine(
  454.                     'http://twitter.com/favicon.ico',
  455.                     'Twitter',
  456.                     getSearchResultsURL(searchTwitterURL, queryTerms));
  457.                 addSearchEngine(
  458.                     'http://delicious.com/favicon.ico',
  459.                     'del.icio.us',
  460.                     getSearchResultsURL(searchDeliciousURL, queryTerms));
  461.             }, "baidu").insertAfter($("body > table", doc)[1]);
  462.         }
  463.         
  464.         var wikipediaURLRegExp = new RegExp("^http://.*\.wikipedia\.org/wiki/(.*)$");
  465.         match = wikipediaURLRegExp.exec(doc.location.href);
  466.         if(match)
  467.         {
  468.             var queryTerms = decodeURIComponent(match[1]).replace(/\+|_/g, " ");
  469.             get_$related_searches_inline(queryTerms, function(addSearchEngine)
  470.             {
  471.                 addSearchEngine(
  472.                     'http://static.smarterfox.com/media/popup_bubble/oneriot-favicon.ico',
  473.                     'OneRiot',
  474.                     getSearchResultsURL(searchOneRiotURL, queryTerms));
  475.                 addSearchEngine(
  476.                     'http://www.yahoo.com/favicon.ico',
  477.                     'Yahoo',
  478.                     getSearchResultsURL(searchYahooURL, queryTerms));
  479.                 var $a = addSearchEngine(
  480.                     'http://www.amazon.com/favicon.ico', 
  481.                     'Amazon', 
  482.                     getSearchResultsURL(searchAmazonURL, queryTerms));
  483.                 $a.click(function()
  484.                 {
  485.                     SW_openNewTab($(this).attr("href"));
  486.                     return false;
  487.                 });
  488.                 addSearchEngine(
  489.                     'http://twitter.com/favicon.ico',
  490.                     'Twitter',
  491.                     getSearchResultsURL(searchTwitterURL, queryTerms));
  492.                 addSearchEngine(
  493.                     'http://delicious.com/favicon.ico',
  494.                     'del.icio.us',
  495.                     getSearchResultsURL(searchDeliciousURL, queryTerms));
  496.             }, "wikipedia").appendTo($("h1#firstHeading", doc));
  497.         }
  498.     };
  499.  
  500.  
  501.  
  502.     var doc = window.document;
  503.     getBoolPref("add_related_searches", function(pref_value)
  504.     {
  505.         if(pref_value)
  506.         {
  507.             add_related_searches(doc);        
  508.         }
  509.     });
  510. }());